package helloRoom;

import com.alibaba.fastjson.JSONObject;
import org.tio.client.intf.ClientAioHandler;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;

import java.io.UnsupportedEncodingException;

/**
 * Created by yunqisong on 2017/08/31.
 */
public class RoomClientHandler extends RoomAbsHandler implements ClientAioHandler {


    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
        RoomPacket helloPacket = (RoomPacket) packet;
        byte[] body = helloPacket.getBody();
        if (body != null) {
            String str = new String(body, RoomConst.CHARSET);
            System.out.println("收到消息：" + str);
        }
    }


    public Packet heartbeatPacket() {
        try {
            return RoomPacket.builder()
                    .body(JSONObject.toJSONString(RoomAgreement.builder().build()).getBytes(RoomConst.CHARSET)).build();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
